package seqlist.stack.leetcode;

import java.util.Stack;

/**
 * 双栈实现最小栈
 * @author yuisama
 * @date 2022/05/09 21:47
 **/
public class Num155_MinStack {
    // s1永远保存实际元素
    private Stack<Integer> s1 = new Stack<>();
    // s2栈顶永远保存当前数据的最小值
    private Stack<Integer> s2 = new Stack<>();

    public void push(int val) {
        s1.push(val);
        if (s2.isEmpty()) {
            s2.push(val);
        }else {
            int peek = s2.peek();
            s2.push(Math.min(val,peek));
        }
    }

    public void pop() {
        s1.pop();
        s2.pop();
    }

    public int top() {
        return s1.peek();
    }

    public int getMin() {
        return s2.peek();
    }
}